package com.tensquare.user.service;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Expression;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import javax.persistence.criteria.Selection;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

import util.IdWorker;

import com.tensquare.user.dao.AdminDao;
import com.tensquare.user.pojo.Admin;

/**
 * 服务层
 * 
 * @author Administrator
 *
 */
@Service
public class AdminService {

	@Autowired
	private AdminDao adminDao;
	
	@Autowired
	private IdWorker idWorker;
	@Autowired
	BCryptPasswordEncoder encoder;

	public List<Admin> findAll() {

		return adminDao.findAll();
	}

	/**
	 * 分页查询
	 * 
	 * @param page
	 * @param size
	 * @return
	 */
	public Page<Admin> findPage(int page, int size) {
		PageRequest pageRequest = new PageRequest(page-1, size);
		return adminDao.findAll(pageRequest);
	}

	private Specification<Admin> where(Map searchMap) {
		
		return new Specification<Admin>() {
          
			@Override
			public Predicate toPredicate(Root<Admin> root,CriteriaQuery<?> query, CriteriaBuilder cb) {
				List<Predicate> predicateList = new ArrayList<Predicate>();
                // ID
                if (searchMap.get("id")!=null && !"".equals(searchMap.get("id"))) {
                	predicateList.add(cb.like(root.get("id").as(String.class), "%"+(String)searchMap.get("id")+"%"));
                }
                // 登陆名称
                if (searchMap.get("loginname")!=null && !"".equals(searchMap.get("loginname"))) {
                	predicateList.add(cb.like(root.get("loginname").as(String.class), "%"+(String)searchMap.get("loginname")+"%"));
                }
                // 密码
                if (searchMap.get("password")!=null && !"".equals(searchMap.get("password"))) {
                	predicateList.add(cb.like(root.get("password").as(String.class), "%"+(String)searchMap.get("password")+"%"));
                }
                // 状态
                if (searchMap.get("state")!=null && !"".equals(searchMap.get("state"))) {
                	predicateList.add(cb.like(root.get("state").as(String.class), "%"+(String)searchMap.get("state")+"%"));
                }

                return cb.and( predicateList.toArray(new Predicate[predicateList.size()]));
                
            }
        };		
		
	}

	public Page<Admin> findSearch(Map whereMap, int page, int size) {
		Specification<Admin> specification = where(whereMap);
		PageRequest pageRequest = new PageRequest(page-1, size);
		return adminDao.findAll(specification, pageRequest);
	}

	public Admin findOne(String id) {
		return adminDao.findById(id).get();
	}

	public void add(Admin admin) {
		admin.setId(idWorker.nextId()+""); //主键值
		String newpassword = encoder.encode(admin.getPassword());//加密后的密码
		admin.setPassword(newpassword);
		adminDao.save(admin);
	}
	
	public void update(Admin admin) {
		adminDao.save(admin);
	}

	public void delete(String id) {
		adminDao.deleteById(id);
	}

	public void deleteList(String[] ids) {
		for (String id : ids) {
			adminDao.deleteById(id);
		}
	}
	/**
	 * 根据登陆名和密码查询
	 * @param loginname
	 * @param password
	 * @return
	 */
	public Admin findByLoginnameAndPassword(String loginname, String
			password){
		Admin admin = adminDao.findByLoginname(loginname);
		if( admin!=null && encoder.matches(password,admin.getPassword())){
			return admin;
		}else{
			return null;
		}
	}

}
